package testes_unitarios;

import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mock.MockLoginServlet;

import org.junit.Test;

public class TestLoginServlet {
	
	private MockLoginServlet servlet = new MockLoginServlet();
	
	@Test
	public void doPostTest() throws ServletException, IOException {
		
		HttpServletRequest request = mock(HttpServletRequest.class);
		HttpServletResponse response = mock(HttpServletResponse.class);
		HttpSession sessao = mock(HttpSession.class);
		RequestDispatcher rq = mock(RequestDispatcher.class);
		
		when(request.getSession(true)).thenReturn(sessao);
		
		when(request.getParameter("email")).thenReturn("karine@mailinator.com");
		when(request.getParameter("senha")).thenReturn("123456");
		
		when(request.getRequestDispatcher(anyString())).thenReturn(rq);
		doNothing().doThrow(new RuntimeException()).when(rq).forward(request, response);
		
		servlet.doPost(request, response);
		
		assertTrue(true);
		
		sessao.invalidate();
	}

}
